package com.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.order.bean.Foods;
import com.order.service.FoodsService;
import com.order.service.impl.FoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "FoodsController", value = "/foods/*")
public class FoodsController extends BaseController {
    private FoodsService foodsService = new FoodsServiceImpl();

    protected void addFood(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
        String json = request.getReader().readLine();
        Foods foods = JSON.parseObject(json, Foods.class);
        boolean added = foodsService.addFood(foods);
        this.writeResult(response, added);
    }

    protected void deleteFoodById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        boolean delete = foodsService.deleteById(id);
        this.writeResult(response, delete);
    }

    protected void deleteFoodBatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idsStr = request.getReader().readLine();
        Integer[] ids = JSON.parseObject(idsStr, Integer[].class);
        boolean batch = foodsService.deleteBatch(ids);
        this.writeResult(response, batch);
    }

    protected void updateFood(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Foods foods = JSON.parseObject(json, Foods.class);
        boolean updated = foodsService.updateFood(foods);
        this.writeResult(response, updated);
    }

    protected void selectFoodById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Foods foods = foodsService.selectFoodById(id);
        this.writeResultAndData(response, foods);
    }

    protected void selectFoodList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Foods foods = JSON.parseObject(json, Foods.class);
        List<Foods> foodsList = foodsService.selcetAllFoods();
        this.writeResultAndData(response, foodsList);
    }

    protected void selectFoodListPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        JSONObject jsonObject = JSON.parseObject(json);
        String name = jsonObject.getString("name");
        Integer pageNum = Integer.valueOf(jsonObject.getString("pageNum"));
        Integer pageSize = Integer.valueOf(jsonObject.getString("pageSize"));
        PageInfo<Foods> foodsPageInfo = foodsService.selcetFoodsPage(name, pageNum, pageSize);
        this.writeResultAndData(response, foodsPageInfo);
    }

    protected void orderFood(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        foodsService.order(foodId, userId);
        this.writeResult(response, true);
    }

}
